package Element;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import Core.KEY;

public class Elemanager {
	class Eles{
		public boolean isFocus;
		public int x, y, w, h;
		public short type;
		
		void setXY(int left, int top){
			x = left; y = top;
		}
		void setBound(int ix, int iy, int i_w, int i_h){
			x = ix; y = iy;
			w = i_w; h = i_h;
		}
		
		protected void paint(Graphics g){}
		protected void destroy(){}
		protected void onResize(){}
		protected void touchDrag(int x, int y) {}
		protected void touchDown(int x, int y) {}
		protected void touchUp(int x, int y, int dragTime, int xBegin, int yBegin) {}
		protected boolean Click(int x, int y) {return true;} // false: click on out side
		protected void update(){}
		protected boolean onkeyDown(int keyCode) { return true;} // false: tra ve cho Manager
	}
	public Vector vec = new Vector();
	public int iFocus;
	
	public void addElement(Eles el){
		vec.addElement(el);
	}
	
	public void setFocus(int index){
		//if(index >= 0 && index < vec.size()) 
		{
			iFocus = index;
			((Eles)vec.elementAt(index)).isFocus = true;
		}
	}
	
	protected void paint(Graphics g){
		for(int i = 0; i < vec.size(); i++) ((Eles)vec.elementAt(i)).paint(g);
	}
	protected void update(){
		for(int i = 0; i < vec.size(); i++) ((Eles)vec.elementAt(i)).update();
	}
	protected void destroy(){
		for(int i = 0; i < vec.size(); i++) ((Eles)vec.elementAt(i)).destroy();
	}
	
//-------------------------------------	begin
	protected void touchDrag(int x, int y) {
		for(int i = 0; i < vec.size(); i++) ((Eles)vec.elementAt(i)).touchDrag(x, y);
	}
	protected void touchDown(int x, int y) {
		for(int i = 0; i < vec.size(); i++) ((Eles)vec.elementAt(i)).touchDown(x, y);
	}
	protected void touchUp(int x, int y, int dragTime, int xBegin, int yBegin) {
		for(int i = 0; i < vec.size(); i++) ((Eles)vec.elementAt(i)).touchUp(x, y, dragTime, xBegin, yBegin);
	}
//------------------------------------- end edit	
	protected void Click(int x, int y) {
		for(int i = vec.size()-1; i > 0; i--) 
			if (((Eles)vec.elementAt(i)).Click(x, y)){
				setFocus(i);
				return;
			}
	}
	protected void onkeyDown(int keyCode) {
		if (!((Eles)vec.elementAt(iFocus)).onkeyDown(keyCode)){
			if(iFocus > 0 && keyCode == KEY.UP) setFocus(iFocus-1);
			else if(iFocus < vec.size()-1 && keyCode == KEY.DOWN) setFocus(iFocus+1);
		}
	}
}
